package com.zhuhjay.chat.server.handler;

import com.zhuhjay.chat.message.ChatRequestMessage;
import com.zhuhjay.chat.message.ChatResponseMessage;
import com.zhuhjay.chat.server.session.SessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;

/**
 * @author ZhuHJay
 * @date 2023/3/7 23:36
 */
@ChannelHandler.Sharable
public class ChatRequestMessageHandler extends AbstractRequestMessageHandler<ChatRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ChatRequestMessage msg) throws Exception {
        Channel channel = SessionFactory.getSession().getChannel(msg.getTo());
        if (channel == null) {
            ctx.writeAndFlush(new ChatResponseMessage(false, "接收用户不存在或不在线"));
            return;
        }
        channel.writeAndFlush(new ChatResponseMessage(msg.getFrom(), msg.getContent()));
    }
}
